<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="昵称" prop="username">
        <el-input v-model.trim="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model.trim="form.realName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="form.sex"
          clearable
          placeholder="请选择"
          style="width: 330px"
        >
          <el-option
            v-for="item in sexList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="校友身份" prop="identity">
        <el-select
          v-model="form.identity"
          clearable
          placeholder="请选择"
          style="width: 330px"
        >
          <el-option
            v-for="item in identityList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月" prop="birthday">
        <el-input v-model.trim="form.birthday" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="专业和班级" prop="majorClass">
        <el-input v-model.trim="form.majorClass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="入学年份" prop="enterTime">
        <el-input v-model.trim="form.enterTime" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="行业领域" prop="occupationalName">
        <el-select
          v-model="form.occupationalId"
          clearable
          placeholder="请选择"
          style="width: 330px"
        >
          <el-option
            v-for="item in occupationalList"
            :key="item.value"
            :label="item.label"
            :value="item.value * 1"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="openID" prop="openId">
        <el-input v-model.trim="form.openId" autocomplete="off"></el-input>
      </el-form-item>

      <!-- <el-form-item label="头像" header-align="center" align="center">
        <template #default="{ row }">
          <el-image
            v-if="imgShow"
            :preview-src-list="imageList"
            :src="row.headUrl"
          ></el-image>
        </template>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    userUpdate,
    occupationalSelectView,
  } from '@/api/management/management'
  export default {
    name: 'UserEdit',
    data() {
      return {
        form: {},
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入昵称' },
          ],
          realName: [
            { required: true, trigger: 'blur', message: '请输入真实姓名' },
          ],
          sex: [{ required: true, trigger: 'blur', message: '请输入性别' }],
          identity: [
            { required: true, trigger: 'blur', message: '请输入校友身份' },
          ],
          birthday: [
            { required: true, trigger: 'blur', message: '请输入出生年月' },
          ],
          majorClass: [
            { required: true, trigger: 'blur', message: '请输入专业和班级' },
          ],
          enterTime: [
            { required: true, trigger: 'blur', message: '请输入入学年份' },
          ],
          occupationalName: [
            { required: true, trigger: 'blur', message: '请输入行业领域' },
          ],
          openId: [
            { required: true, trigger: 'blur', message: '请输入openId' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        occupationalList: [],
        identityList: [
          {
            value: '就读',
            label: '就读',
          },
          {
            value: '教职工',
            label: '教职工',
          },
        ],
        sexList: [
          {
            value: '1',
            label: '男',
          },
          {
            value: '0',
            label: '女',
          },
        ],
        row: {}, //用于判断编辑or添加
      }
    },
    created() {
      this.getOccupationalList()
    },
    methods: {
      showEdit(row) {
        this.row = row
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            //判断编辑or添加
            if (this.row) {
              await userUpdate(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage(res.data, 'error')
                }
              })
            } else {
              await userAdd(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage(res.data, 'error')
                }
              })
            }
            //置空
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('fetchData')
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
      //获取 行业领域
      async getOccupationalList() {
        await occupationalSelectView().then((res) => {
          if (res.code == 200) {
            this.occupationalList = res.data
          } else {
            this.$baseMessage('行业领域获取失败', 'error')
          }
        })
      },
    },
  }
</script>
